#encoding:utf-8

import csv
import datetime
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.ns import qn

class Config :
    leader = ""
    start_time = ""
    end_time = ""
    output_file_name = ""

    def __init__(self, list):
        self.leader = list[0][0]
        self.start_time = list[1][0]
        self.end_time = list[1][1]
        self.output_file_name = list[2][0]

    def display(self):
        print("leader = %s" % (self.leader))
        print("start_time = %s" % (self.start_time))
        print("end_time = %s" % (self.end_time))
        print("output_file_name = %s" % (self.output_file_name))


def read_csv_to_list(path):
    try:
        with open(path, "r", encoding="utf-8") as csv_config_file:
            reader = csv.reader(csv_config_file)
            result = []

            for row in reader:
                result.append(row)
                print(row)
            return result
    except FileNotFoundError:
        print("Err: %s is not found." % (path))
    except PermissionError:
        print("Err: You don't have permission to access this file.")

config_lists = read_csv_to_list("config.csv")
member_lists = read_csv_to_list("member.csv")

config = Config(config_lists)
config.display()


content = '''尊敬的领导:\n       我是信息学院计算机协会负责人%s，我们协会旨在提高我系同学的专业编程水平，为每年的湖南省程序设计竞赛做准备。因培训安排在周六周日和每天晚上7点到9点半，与学校的常规晚自习或者一些学校举行的活动冲突，敬请谅解。特此请假。请假时间为%s至%s。\n       望领导批准！\n       具体请假人员为:'''



document = Document()

document.styles["Normal"].font.name = u"等线"
document.styles["Normal"]._element.rPr.rFonts.set(qn("w:eastAsia"), u"等线")

heading = document.add_heading('', level=1)
run = heading.add_run(u"请假条")
run.font.name = u"等线"
run.font.size = Pt(22)
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'等线')

heading.alignment = WD_ALIGN_PARAGRAPH.CENTER



paragraph = document.add_paragraph(text=content % (config.leader, config.start_time, config.end_time), style='Normal')

college_list = []
subject_list = []
member_list = []

for list in member_lists:
    if not list[0] in college_list:
        college_list.append(list[0])

for list in member_lists:
    if not list[1] in subject_list and list[0] == college_list[0]:
        subject_list.append(list[1])

for college in college_list:
    subject_list.clear()
    college_head = "%s :" % (college)
    for member in member_lists:
        if not member[1] in subject_list and member[0] == college:
            subject_list.append(member[1])

    document.add_paragraph(text=college_head)

    for subject in subject_list:
        subject_head = "\t%s :" % (subject)
        for member in member_lists:
            if member[0] == college and member[1] == subject:
                subject_head += member[2]
                subject_head += "  "
        document.add_paragraph(text=subject_head)


document.add_paragraph(text="\nCAS---ACM协会负责老师签名:", style="Normal")
date_paragraph = document.add_paragraph(text="%s年%s月%s日" % (datetime.date.today().year, datetime.date.today().month, datetime.date.today().day), style="Normal")
date_paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

cas = document.add_paragraph(text="人文CAS---ACM协会", style="Normal")
cas.alignment = WD_ALIGN_PARAGRAPH.RIGHT

document.save("%s.docx" % config.output_file_name)
print("Done!")